package com.cmcc.passenger.ui.places

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.cmcc.shared.adapter.SingleItemViewBinder
import com.cmcc.passenger.R
import com.cmcc.passenger.data.entity.PlaceAutoComplete
import com.cmcc.passenger.databinding.ListItemGetResultBinding


class PlaceGetViewBinder :
    SingleItemViewBinder<PlaceAutoComplete, PlaceGetViewHolder>(PlaceAutoComplete::class.java) {

    private var onItemClickListener: OnItemGetClickListener<ListItemGetResultBinding, PlaceAutoComplete>? =
        null
    private var favoriteClickListener: OnItemGetClickListener<ListItemGetResultBinding, PlaceAutoComplete>? =
        null

    override fun createViewHolder(parent: ViewGroup): RecyclerView.ViewHolder =
        PlaceGetViewHolder(
            ListItemGetResultBinding.inflate(
                LayoutInflater.from(parent.context),
                parent,
                false
            )
        )

    override fun bindViewHolder(model: PlaceAutoComplete, viewHolder: PlaceGetViewHolder) {
        viewHolder.bind(
            createOnItemClickListener(viewHolder.binding, model),
            createOnFavoriteClickListener(viewHolder.binding, model),
            model
        )
    }

    fun setOnItemClickListener(listener: OnItemGetClickListener<ListItemGetResultBinding, PlaceAutoComplete>) {
        this.onItemClickListener = listener
    }

    fun setOnFavoriteClickListener(listener: OnItemGetClickListener<ListItemGetResultBinding, PlaceAutoComplete>) {
        this.favoriteClickListener = listener
    }

    private fun createOnItemClickListener(
        binding: ListItemGetResultBinding,
        model: PlaceAutoComplete
    ): View.OnClickListener {
        return View.OnClickListener { onItemClickListener?.onItemClick(binding, model) }
    }

    private fun createOnFavoriteClickListener(
        binding: ListItemGetResultBinding,
        model: PlaceAutoComplete
    ): View.OnClickListener {
        return View.OnClickListener { favoriteClickListener?.onItemClick(binding, model) }
    }

    override fun areItemsTheSame(oldItem: PlaceAutoComplete, newItem: PlaceAutoComplete): Boolean =
        oldItem == newItem

    override fun areContentsTheSame(
        oldItem: PlaceAutoComplete,
        newItem: PlaceAutoComplete
    ): Boolean =
        oldItem == newItem

}

interface OnItemGetClickListener<in B,in T> {
    fun onItemClick(view: B, model: T)
}

class PlaceGetViewHolder(val binding: ListItemGetResultBinding) :
    RecyclerView.ViewHolder(binding.root) {
    fun bind(
        listener: View.OnClickListener,
        favoriteListener: View.OnClickListener,
        model: PlaceAutoComplete
    ) {
        if(model.isAdded) binding.ivFavorite.setImageResource(R.drawable.ic_favorite) else binding.ivFavorite.setImageResource(R.drawable.ic_unfavorite)
        with(binding) {
            clickListener = listener
            favorite = favoriteListener
            viewModel = PlaceItemListViewModel(model)
            executePendingBindings()
        }
    }
}